package com.soap.webservice.interceptors;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/4/18 14:11
 */
public class CheckSoapHeader extends AbstractPhaseInterceptor<SoapMessage> {

    public CheckSoapHeader() {
        super(Phase.PRE_INVOKE);
    }

    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        System.out.println("handle inbound soap message");
        final Header auth = message.getHeader(new QName("http://crscd.com.cn/auth", "auth"));
        if (auth != null) {
            final Element element = (Element) auth.getObject();
            final String username = element.getElementsByTagName("db.username").item(0).getTextContent();
            System.out.println("username:" + username);
        }

    }
}
